package com.fly.boot.service;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.fly.boot.utils.LineParser;
import com.fly.boot.utils.ShellExecutor;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class DockerService
{
    private String dockerCmd = "docker ps --format \"{{.Names}} {{.Ports}}\"";
    
    /**
     * 获取docker相关信息
     * 
     * @return
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    public Map<String, Set<String>> getDockerInfo()
        throws IOException
    {
        int key = (int)System.currentTimeMillis() % 2;
        log.info("key: {}", key);
        switch (key)
        {
            case 0:
                Map<String, Set<String>> map = new TreeMap<>();
                ShellExecutor.exec(dockerCmd).stream().forEach(line -> map.put(LineParser.key(line), LineParser.value(line)));
                return map;
            
            case 1:
            default:
                return new TreeMap<>(ShellExecutor.exec(dockerCmd).stream().collect(Collectors.toMap(LineParser::key, LineParser::value, (key1, key2) -> key2)));
        }
    }
}
